#pragma once

#include "mqtt_client_interface.h"

#include <MQTTClient.h>
#include <string>

class MQTTClientImplement : public MQTTClientInterface
{
public:
    MQTTClientImplement(std::string server_uri,
                        std::string client_id,
                        std::string username,
                        std::string password,
                        std::string ca_file,
                        std::string cert_file,
                        std::string key_file,
                        std::string key_file_password,
                        int         connect_timeout_s);
    ~MQTTClientImplement();

private:
    static void connection_lost_callback(void *context, char *cause);
    static int  message_arrived_callback(void *context, char *topicName, int topicLen, MQTTClient_message *message);
    static void delivery_complete_callback(void *context, MQTTClient_deliveryToken dt);
    static int  openssl_error_callback(const char *str, size_t len, void *u);

public:
    bool connect() override;
    void disconnect() override;
    bool subscribe(const std::string &topic, int qos) override;
    bool unsubscribe(const std::string &topic) override;
    bool publish(const std::string &topic, const std::string &data, int qos) override;

private:
    MQTTClient_connectOptions connect_opts_;
    MQTTClient_SSLOptions     connect_ssl_opts_;
    MQTTClient                client_;

private:
    std::string server_uri_;
    std::string client_id_;
    std::string client_username_;
    std::string client_password_;
    std::string client_ssl_filepath_ca_;
    std::string client_ssl_filepath_client_cert_;
    std::string client_ssl_filepath_client_key_;
    std::string client_ssl_filepath_client_key_password_;
    int         connect_timeout_s_;
};
